package com.easy.controller;

import com.easy.bean.User;
import com.easy.common.CommonResult;
import com.easy.config.WebConfig;
import com.easy.dao.IUserDao;
import com.easy.service.IUserService;
import com.easy.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    IUserService userS;
    @Autowired
    IUserDao dao;


    @GetMapping("username")
    public CommonResult getUserByUsername(User user){
        List<User> list=userS.getUserByUsername(user.getUsername());
        return  CommonResult.success(list);
    }

    @PostMapping("dologin")
    public CommonResult doLogin(@RequestBody Uservalue user, HttpSession session){
        System.out.println(user.getPassword());
        System.out.println(user.getUsername());
        String token=userS.doLogin(user);
        String captcha=(String) session.getAttribute("captcha");

        if(!captcha.equalsIgnoreCase(user.getCaptcha())){
            return CommonResult.fail("验证码错误");
        }


        if(token==null){
            session.setAttribute("user",user);
            return CommonResult.fail(403,"登陆失败");

        }else {
            return CommonResult.success(200,"登陆成功",token);
        }
    }


    @GetMapping("checktoken")
    public CommonResult checkToken(HttpServletRequest request){
        String token=request.getHeader("Authorization");
        System.out.println(token);
        if(token!=null&& JWTUtil.verifyToken(token)){
            return CommonResult.success();
        }
        return CommonResult.fail();
    }

    @GetMapping("user")
    public CommonResult getUser(User user, Page page){
        List<User> list=userS.getUser(user,page);
        int count=userS.getCount(user);
        HashMap map=new HashMap();
        map.put("list",list);
        map.put("count",count);
        //System.out.println(list);
        return CommonResult.success(map);
    }
    @PostMapping("user")
    public CommonResult addUserJSON(@RequestBody User user){
        //System.out.println(user);
        userS.addUser(user);
        user=userS.getUserById(user.getId());
        return CommonResult.success(user);
    }
    @DeleteMapping("user/{id}")
    public CommonResult delUser(@PathVariable int id){
        int count=dao.delUser(id);
        if(count>0){
            return CommonResult.success();
        }else {
            return CommonResult.fail();
        }
    }
    @PutMapping("user")
    public CommonResult editUserJSON(@RequestBody User user){
        String result = user.getImg().substring(user.getImg().lastIndexOf('/') + 1);
        user.setImg(result);
        System.out.println(user);
        userS.editUser(user);
        user=userS.getUserById(user.getId());
        return CommonResult.success(user);
    }

    @PostMapping("userimg")
    public CommonResult userImg( @RequestParam("file") MultipartFile file) {
        //存储文件
        String newName= UploadFileUtil.uploadfile(file);
        //将文件地址存入数据库
        //返回上传文件的地址 返回上传头像的员工数据
        return CommonResult.success(WebConfig.STATIC_PATH+newName);
    }

    @GetMapping("userimg")
    public CommonResult userImg(User user) {
        User users = userS.getImg(user);
        return CommonResult.success(users);
    }

    @PostMapping("register")
    public CommonResult handleRegister(@RequestBody User user){
        System.out.println(user);
        try {
            userS.Reg(user);
            return CommonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.fail(0,e.getMessage());
        }
    }




    @PutMapping("updatePassword")
    public CommonResult updatePassword(@RequestParam("username") String username, @RequestParam("password") String password) {

        // 获取用户
        List<User> userList = userS.getUserByUsername(username);

        // 检查用户是否存在
        if (userList.isEmpty()) {
            return CommonResult.fail();
        }

        // 更新第一个匹配的用户
        User user = userList.get(0);
        user.setPassword(DigestUtil.randomDigest(password));
        userS.editUser(user);

        return CommonResult.success();
    }

    @GetMapping("password")
    public CommonResult confirmPassword(@RequestParam("username") String username, @RequestParam("password") String password) {
        // 获取用户
        List<User> userList = userS.getUserByUsername(username);
        // 检查用户是否存在
        if (userList.isEmpty()) {
            return CommonResult.fail();
        }
        User user = userList.get(0);

        boolean bool=DigestUtil.decode(password,user.getPassword());

        if (bool) {
            return CommonResult.success();
        }
        return CommonResult.fail();
    }


//    @PutMapping("/updatePassword")
//    public CommonResult updatePassword(@RequestParam("username") String username, @RequestParam("password") String password) {
//        System.out.println(username+password);
//        // 获取用户
//        List<User> userList = userS.getUserByUsername(username);
//
//        // 检查用户是否存在
//        if (userList.isEmpty()) {
//            return CommonResult.fail();
//        }
//
//        // 更新第一个匹配的用户
//        User user = userList.get(0);
//        user.setPassword(DigestUtil.randomDigest(password));
//        userS.editUser(user);
//
//        return CommonResult.success();
//    }
//
//    @GetMapping("/password")
//    public CommonResult confirmPassword(@RequestParam( "username") String username, @RequestParam("password") String password,
//                                        @RequestParam("newpassword")String newpassword
//    ){
//
//        try {
//            userS.editPassword(username,password,newpassword);
//
//            return CommonResult.success();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return CommonResult.fail();
//
//        }
//
//    }

    /*{
        // 获取用户
        List<User> userList = userS.getUserByUsername(username);
        // 检查用户是否存在
        if (userList.isEmpty()) {


            return CommonResult.fail();
        }

        User user = userList.get(0);

        boolean bool=DigestUtil.decode(password,user.getPassword());

        if (bool) {
            return CommonResult.success();
        }
        return CommonResult.fail();
    }*/
}









